import {
  PersonModel
} from '../../models/hdd/personal.js'
let personModel = new PersonModel()
import {
  LoginModel
} from '../../models/hdd/login.js'
let loginModel = new LoginModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyName: '',
    password: '',
    confirmPwd: '',
    recMobile: '',
    mobile: '',
  },

  nameInput(e) {
    this.setData({
      companyName: e.detail.value
    })
  },

  passwordInput(e) {
    this.setData({
      password: e.detail.value
    })
  },

  confirmInput(e) {
    this.setData({
      confirmPwd: e.detail.value
    })
  },

  recMobileInput(e) {
    this.setData({
      recMobile: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '公司资料完善'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF7136',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.setData({
      mobile: options.mobile
    })
  },

  checkData() {
    if (!this.data.companyName) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写企业账号'
      })
      return
    }
    if (!this.data.password) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写登陆密码'
      })
      return
    }
    if (!this.data.confirmPwd) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写密码确认'
      })
      return
    }
    if (this.data.confirmPwd !== this.data.password) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '两次填写密码不一致，请重新填写'
      })
      return
    }
    wx.showLoading()
    this.register()
  },

  register() {
    loginModel.registerUser({
      args: {
        FMobile: this.data.mobile,
        data: {
          FMobile: this.data.mobile,
          companyAccount: this.data.companyName,
          password: this.data.password,
          referrerMobile: this.data.recMobile
        },
        userType: 'company'
      }
    }).then(res => {
      wx.setStorageSync('token', res.data.data.token)
      wx.hideLoading()
      wx.navigateTo({
        url: `/pages/perfectCompanyInfo/perfectCompanyInfo?userId=${res.data.data.id}&mobile=${this.data.mobile}`,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})